home *** CD-ROM | disk | FTP | other *** search
/ Champak 74 / Volume 74 My Disc - Damaged.iso / Games / shape_escape.swf / scripts / mochi / MochiServices.as < prev   
Text File  |  2008-08-07  |  17KB  |  562 lines

  1. package mochi
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.display.Loader;
  5.    import flash.display.MovieClip;
  6.    import flash.display.Sprite;
  7.    import flash.events.Event;
  8.    import flash.events.IOErrorEvent;
  9.    import flash.events.StatusEvent;
  10.    import flash.events.TimerEvent;
  11.    import flash.net.LocalConnection;
  12.    import flash.net.URLRequest;
  13.    import flash.system.Security;
  14.    import flash.utils.Timer;
  15.    import flash.utils.getTimer;
  16.    
  17.    public class MochiServices
  18.    {
  19.       
  20.       private static var _container:Object;
  21.       
  22.       private static var _connected:Boolean = false;
  23.       
  24.       private static var _swfVersion:String;
  25.       
  26.       private static var _sendChannel:LocalConnection;
  27.       
  28.       private static var _rcvChannelName:String;
  29.       
  30.       private static var _gatewayURL:String = "http://www.mochiads.com/static/lib/services/services.swf";
  31.       
  32.       private static var _clip:MovieClip;
  33.       
  34.       private static var _loader:Loader;
  35.       
  36.       private static var _id:String;
  37.       
  38.       private static var _listenChannel:LocalConnection;
  39.       
  40.       private static var _timer:Timer;
  41.       
  42.       private static var _sendChannelName:String;
  43.       
  44.       private static var _startTime:Number;
  45.       
  46.       private static var _connecting:Boolean = false;
  47.       
  48.       public static var onError:Object;
  49.       
  50.       private static var _listenChannelName:String = "__mochiservices";
  51.       
  52.       private static var _rcvChannel:LocalConnection;
  53.        
  54.       
  55.       public function MochiServices()
  56.       {
  57.          super();
  58.       }
  59.       
  60.       public static function isNetworkAvailable() : Boolean
  61.       {
  62.          return Security.sandboxType != "localWithFile";
  63.       }
  64.       
  65.       public static function send(param1:String, param2:Object = null, param3:Object = null, param4:Object = null) : void
  66.       {
  67.          if(_connected)
  68.          {
  69.             _sendChannel.send(_sendChannelName,"onReceive",{
  70.                "methodName":param1,
  71.                "args":param2,
  72.                "callbackID":_clip._nextcallbackID
  73.             });
  74.          }
  75.          else
  76.          {
  77.             if(_clip == null || !_connecting)
  78.             {
  79.                onError("NotConnected");
  80.                handleError(param2,param3,param4);
  81.                flush(true);
  82.                return;
  83.             }
  84.             _clip._queue.push({
  85.                "methodName":param1,
  86.                "args":param2,
  87.                "callbackID":_clip._nextcallbackID
  88.             });
  89.          }
  90.          if(_clip != null)
  91.          {
  92.             if(_clip._callbacks != null && _clip._nextcallbackID != null)
  93.             {
  94.                _clip._callbacks[_clip._nextcallbackID] = {
  95.                   "callbackObject":param3,
  96.                   "callbackMethod":param4
  97.                };
  98.                ++_clip._nextcallbackID;
  99.             }
  100.          }
  101.       }
  102.       
  103.       public static function get connected() : Boolean
  104.       {
  105.          return _connected;
  106.       }
  107.       
  108.       private static function flush(param1:Boolean) : void
  109.       {
  110.          var _loc2_:Object = null;
  111.          var _loc3_:Object = null;
  112.          if(_clip != null)
  113.          {
  114.             if(_clip._queue != null)
  115.             {
  116.                while(_clip._queue.length > 0)
  117.                {
  118.                   _loc2_ = _clip._queue.shift();
  119.                   _loc3_ = null;
  120.                   if(_loc2_ != null)
  121.                   {
  122.                      if(_loc2_.callbackID != null)
  123.                      {
  124.                         _loc3_ = _clip._callbacks[_loc2_.callbackID];
  125.                      }
  126.                      delete _clip._callbacks[_loc2_.callbackID];
  127.                      if(param1 && _loc3_ != null)
  128.                      {
  129.                         handleError(_loc2_.args,_loc3_.callbackObject,_loc3_.callbackMethod);
  130.                      }
  131.                   }
  132.                }
  133.             }
  134.          }
  135.       }
  136.       
  137.       private static function init(param1:String, param2:Object) : void
  138.       {
  139.          _id = param1;
  140.          if(param2 != null)
  141.          {
  142.             _container = param2;
  143.             loadCommunicator(param1,_container);
  144.          }
  145.       }
  146.       
  147.       public static function get childClip() : Object
  148.       {
  149.          return _clip;
  150.       }
  151.       
  152.       public static function get id() : String
  153.       {
  154.          return _id;
  155.       }
  156.       
  157.       public static function stayOnTop() : void
  158.       {
  159.          _container.addEventListener(Event.ENTER_FRAME,MochiServices.bringToTop,false,0,true);
  160.          if(_clip != null)
  161.          {
  162.             _clip.visible = true;
  163.          }
  164.       }
  165.       
  166.       public static function getVersion() : String
  167.       {
  168.          return "1.31";
  169.       }
  170.       
  171.       public static function disconnect() : void
  172.       {
  173.          if(_connected || _connecting)
  174.          {
  175.             if(_clip != null)
  176.             {
  177.                if(_clip.parent != null)
  178.                {
  179.                   if(_clip.parent is Sprite)
  180.                   {
  181.                      Sprite(_clip.parent).removeChild(_clip);
  182.                      _clip = null;
  183.                   }
  184.                }
  185.             }
  186.             _connecting = _connected = false;
  187.             flush(true);
  188.             try
  189.             {
  190.                _listenChannel.close();
  191.                _rcvChannel.close();
  192.             }
  193.             catch(error:Error)
  194.             {
  195.             }
  196.          }
  197.          if(_timer != null)
  198.          {
  199.             try
  200.             {
  201.                _timer.stop();
  202.             }
  203.             catch(error:Error)
  204.             {
  205.             }
  206.          }
  207.       }
  208.       
  209.       public static function allowDomains(param1:String) : String
  210.       {
  211.          var _loc2_:String = null;
  212.          Security.allowDomain("*");
  213.          Security.allowInsecureDomain("*");
  214.          if(param1.indexOf("http://") != -1)
  215.          {
  216.             _loc2_ = param1.split("/")[2].split(":")[0];
  217.             Security.allowDomain(_loc2_);
  218.             Security.allowInsecureDomain(_loc2_);
  219.          }
  220.          return _loc2_;
  221.       }
  222.       
  223.       public static function doClose() : void
  224.       {
  225.          _container.removeEventListener(Event.ENTER_FRAME,MochiServices.bringToTop);
  226.          if(_clip.parent != null)
  227.          {
  228.             Sprite(_clip.parent).removeChild(_clip);
  229.          }
  230.       }
  231.       
  232.       public static function setContainer(param1:Object = null, param2:Boolean = true) : void
  233.       {
  234.          if(param1 != null)
  235.          {
  236.             if(param1 is Sprite)
  237.             {
  238.                _container = param1;
  239.             }
  240.          }
  241.          if(param2)
  242.          {
  243.             if(_container is Sprite)
  244.             {
  245.                Sprite(_container).addChild(_clip);
  246.             }
  247.          }
  248.       }
  249.       
  250.       private static function onStatus(param1:StatusEvent) : void
  251.       {
  252.          switch(param1.level)
  253.          {
  254.             case "error":
  255.                _connected = false;
  256.                _listenChannel.connect(_listenChannelName);
  257.          }
  258.       }
  259.       
  260.       private static function initComChannels() : void
  261.       {
  262.          if(!_connected)
  263.          {
  264.             _sendChannel.addEventListener(StatusEvent.STATUS,MochiServices.onStatus);
  265.             _sendChannel.send(_sendChannelName,"onReceive",{"methodName":"handshakeDone"});
  266.             _sendChannel.send(_sendChannelName,"onReceive",{
  267.                "methodName":"registerGame",
  268.                "id":_id,
  269.                "clip":_container,
  270.                "version":getVersion()
  271.             });
  272.             _rcvChannel.addEventListener(StatusEvent.STATUS,MochiServices.onStatus);
  273.             _clip.onReceive = function(param1:Object):void
  274.             {
  275.                var cb:String = null;
  276.                var cblst:Object = null;
  277.                var method:* = undefined;
  278.                var methodName:String = null;
  279.                var obj:Object = null;
  280.                var pkg:Object = param1;
  281.                cb = pkg.callbackID;
  282.                cblst = this.client._callbacks[cb];
  283.                if(!cblst)
  284.                {
  285.                   return;
  286.                }
  287.                method = cblst.callbackMethod;
  288.                methodName = "";
  289.                obj = cblst.callbackObject;
  290.                if(obj && typeof method == "string")
  291.                {
  292.                   methodName = method;
  293.                   if(obj[method] != null)
  294.                   {
  295.                      method = obj[method];
  296.                   }
  297.                   else
  298.                   {
  299.                      trace("Error: Method  " + method + " does not exist.");
  300.                   }
  301.                }
  302.                if(method != undefined)
  303.                {
  304.                   try
  305.                   {
  306.                      method.apply(obj,pkg.args);
  307.                   }
  308.                   catch(error:Error)
  309.                   {
  310.                      trace("Error invoking callback method \'" + methodName + "\': " + error.toString());
  311.                   }
  312.                }
  313.                else if(obj != null)
  314.                {
  315.                   try
  316.                   {
  317.                      obj(pkg.args);
  318.                   }
  319.                   catch(error:Error)
  320.                   {
  321.                      trace("Error invoking method on object: " + error.toString());
  322.                   }
  323.                }
  324.                delete this.client._callbacks[cb];
  325.             };
  326.             _clip.onError = function():void
  327.             {
  328.                MochiServices.onError("IOError");
  329.             };
  330.             _rcvChannel.connect(_rcvChannelName);
  331.             trace("connected!");
  332.             _connecting = false;
  333.             _connected = true;
  334.             _listenChannel.close();
  335.             while(_clip._queue.length > 0)
  336.             {
  337.                _sendChannel.send(_sendChannelName,"onReceive",_clip._queue.shift());
  338.             }
  339.          }
  340.       }
  341.       
  342.       private static function listen() : void
  343.       {
  344.          _listenChannel = new LocalConnection();
  345.          _listenChannel.client = _clip;
  346.          _clip.handshake = function(param1:Object):void
  347.          {
  348.             MochiServices.comChannelName = param1.newChannel;
  349.          };
  350.          _listenChannel.allowDomain("*","localhost");
  351.          _listenChannel.allowInsecureDomain("*","localhost");
  352.          _listenChannel.connect(_listenChannelName);
  353.          trace("Waiting for MochiAds services to connect...");
  354.       }
  355.       
  356.       private static function handleError(param1:Object, param2:Object, param3:Object) : void
  357.       {
  358.          var args:Object = param1;
  359.          var callbackObject:Object = param2;
  360.          var callbackMethod:Object = param3;
  361.          if(args != null)
  362.          {
  363.             if(args.onError != null)
  364.             {
  365.                args.onError.apply(null,["NotConnected"]);
  366.             }
  367.          }
  368.          if(callbackMethod != null)
  369.          {
  370.             args = {};
  371.             args.error = true;
  372.             args.errorCode = "NotConnected";
  373.             if(callbackObject != null && callbackMethod is String)
  374.             {
  375.                try
  376.                {
  377.                   callbackObject[callbackMethod](args);
  378.                }
  379.                catch(error:Error)
  380.                {
  381.                }
  382.             }
  383.             else if(callbackMethod != null)
  384.             {
  385.                try
  386.                {
  387.                   callbackMethod.apply(args);
  388.                }
  389.                catch(error:Error)
  390.                {
  391.                }
  392.             }
  393.          }
  394.       }
  395.       
  396.       public static function get clip() : Object
  397.       {
  398.          return _container;
  399.       }
  400.       
  401.       public static function set comChannelName(param1:String) : void
  402.       {
  403.          if(param1 != null)
  404.          {
  405.             if(param1.length > 3)
  406.             {
  407.                _sendChannelName = param1 + "_fromgame";
  408.                _rcvChannelName = param1;
  409.                initComChannels();
  410.             }
  411.          }
  412.       }
  413.       
  414.       private static function loadCommunicator(param1:String, param2:Object) : MovieClip
  415.       {
  416.          var clipname:String = null;
  417.          var f:Function = null;
  418.          var req:URLRequest = null;
  419.          var id:String = param1;
  420.          var clip:Object = param2;
  421.          clipname = "_mochiservices_com_" + id;
  422.          if(_clip != null)
  423.          {
  424.             return _clip;
  425.          }
  426.          if(!MochiServices.isNetworkAvailable())
  427.          {
  428.             return null;
  429.          }
  430.          MochiServices.allowDomains(_gatewayURL);
  431.          _clip = createEmptyMovieClip(clip,clipname,10336,false);
  432.          _loader = new Loader();
  433.          _timer = new Timer(1000,0);
  434.          _startTime = getTimer();
  435.          _timer.addEventListener(TimerEvent.TIMER,connectWait);
  436.          _timer.start();
  437.          f = function(param1:Object):void
  438.          {
  439.             _clip._mochiad_ctr_failed = true;
  440.             trace("MochiServices could not load.");
  441.             MochiServices.disconnect();
  442.             MochiServices.onError("IOError");
  443.          };
  444.          _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,f);
  445.          req = new URLRequest(_gatewayURL);
  446.          _loader.load(req);
  447.          _clip.addChild(_loader);
  448.          _clip._mochiservices_com = _loader;
  449.          _sendChannel = new LocalConnection();
  450.          _clip._queue = [];
  451.          _rcvChannel = new LocalConnection();
  452.          _rcvChannel.allowDomain("*","localhost");
  453.          _rcvChannel.allowInsecureDomain("*","localhost");
  454.          _rcvChannel.client = _clip;
  455.          _clip._nextcallbackID = 0;
  456.          _clip._callbacks = {};
  457.          listen();
  458.          return _clip;
  459.       }
  460.       
  461.       public static function bringToTop(param1:Event) : void
  462.       {
  463.          var e:Event = param1;
  464.          if(MochiServices.clip != null)
  465.          {
  466.             if(MochiServices.childClip != null)
  467.             {
  468.                try
  469.                {
  470.                   if(MochiServices.clip.numChildren > 1)
  471.                   {
  472.                      MochiServices.clip.setChildIndex(MochiServices.childClip,MochiServices.clip.numChildren - 1);
  473.                   }
  474.                }
  475.                catch(errorObject:Error)
  476.                {
  477.                   trace("Warning: Depth sort error.");
  478.                   _container.removeEventListener(Event.ENTER_FRAME,MochiServices.bringToTop);
  479.                }
  480.             }
  481.          }
  482.       }
  483.       
  484.       public static function connect(param1:String, param2:Object, param3:Object = null) : void
  485.       {
  486.          var id:String = param1;
  487.          var clip:Object = param2;
  488.          var onError:Object = param3;
  489.          if(clip is DisplayObject)
  490.          {
  491.             if(!_connected && _clip == null)
  492.             {
  493.                trace("MochiServices Connecting...");
  494.                _connecting = true;
  495.                init(id,clip);
  496.             }
  497.          }
  498.          else
  499.          {
  500.             trace("Error, MochiServices requires a Sprite, Movieclip or instance of the stage.");
  501.          }
  502.          if(onError != null)
  503.          {
  504.             MochiServices.onError = onError;
  505.          }
  506.          else if(MochiServices.onError == null)
  507.          {
  508.             MochiServices.onError = function(param1:String):void
  509.             {
  510.                trace(param1);
  511.             };
  512.          }
  513.       }
  514.       
  515.       public static function createEmptyMovieClip(param1:Object, param2:String, param3:Number, param4:Boolean = true) : MovieClip
  516.       {
  517.          var mc:MovieClip = null;
  518.          var parent:Object = param1;
  519.          var name:String = param2;
  520.          var depth:Number = param3;
  521.          var doAdd:Boolean = param4;
  522.          mc = new MovieClip();
  523.          if(doAdd)
  524.          {
  525.             if(false && depth)
  526.             {
  527.                parent.addChildAt(mc,depth);
  528.             }
  529.             else
  530.             {
  531.                parent.addChild(mc);
  532.             }
  533.          }
  534.          try
  535.          {
  536.             parent[name] = mc;
  537.          }
  538.          catch(e:Error)
  539.          {
  540.             throw new Error("MochiServices requires a clip that is an instance of a dynamic class.  If your class extends Sprite or MovieClip, you must make it dynamic.");
  541.          }
  542.          mc["_name"] = name;
  543.          return mc;
  544.       }
  545.       
  546.       public static function connectWait(param1:TimerEvent) : void
  547.       {
  548.          if(getTimer() - _startTime > 10000)
  549.          {
  550.             if(!_connected)
  551.             {
  552.                _clip._mochiad_ctr_failed = true;
  553.                trace("MochiServices could not load.");
  554.                MochiServices.disconnect();
  555.                MochiServices.onError("IOError");
  556.             }
  557.             _timer.stop();
  558.          }
  559.       }
  560.    }
  561. }
  562.